package com.lcj.aoch.rule.service.impl;

import cn.hutool.core.map.MapUtil;
import com.lcj.aoch.rule.db.dao.AwardRuleDao;
import com.lcj.aoch.rule.db.dao.OrderDao;
import com.lcj.aoch.rule.db.pojo.AwardRuleEntity;
import com.lcj.aoch.rule.service.AwardRuleService;
import com.lcj.common.exception.AochException;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

@Service
public class AwardRuleImpl implements AwardRuleService {
  private static final Logger log = LoggerFactory.getLogger(AwardRuleImpl.class);
  
  @Resource
  private OrderDao orderDao;
  
  @Resource
  private AwardRuleDao awardRuleDao;
  
  public String calculateIncentiveFee(long driverId, String acceptTime, String msg) {
    AwardRuleEntity awardRuleEntity = awardRuleDao.searchCurrentRule(msg);
    String rule = awardRuleEntity.getRule();
    ExpressRunner expressRunner = new ExpressRunner();
    DefaultContext defaultContext = new DefaultContext();
    defaultContext.put("flag", Boolean.valueOf(false));
    defaultContext.put("acceptTime", acceptTime);
    try {
      HashMap<String, Long> hashMap = (HashMap)expressRunner.execute(rule, (IExpressContext)defaultContext, null, true, false);
      hashMap.put("cancelRuleId", awardRuleEntity.getId());
      if (hashMap.size() == 1) {
        String start = MapUtil.getStr(hashMap, "start");
        String var8 = MapUtil.getStr(hashMap, "end");
        HashMap<Object, Object> hashMap2 = new HashMap<>();
        hashMap2.put("driverId", Long.valueOf(driverId));
        hashMap2.put("start", start);
        hashMap2.put("end", var8);
        long l = this.orderDao.searchFinishCountInRange(hashMap2);
        if (l == 0L)
          return "0.00"; 
        defaultContext.replace("flag", Boolean.valueOf(true));
        defaultContext.put("real", Long.valueOf(l));
        return (String)expressRunner.execute(rule, (IExpressContext)defaultContext, null, true, false);
      } 
      return "0.00";
    } catch (Exception var9) {
      log.error("计算系统奖励失败", var9);
      throw new AochException("计算系统奖励失败");
    } 
  }
}
